<template>
	<div class="data-box">
		<div class="data-title">最新版本信息查询</div>

		<el-form class="data-search-area" :model="searchForm" @submit.native.prevent label-width="80px" style="height: auto">
			<el-row>
				<!-- <el-col :span="6">
					<el-form-item label="活动ID" prop="activityId">
						<el-input type="text" v-model="searchForm.activityId" placeholder="请输入活动ID" clearable @keyup.enter.native="submitForm"></el-input>
					</el-form-item>
				</el-col> -->

				<el-col :span="6">
					<div class="search-submit">
						<el-button type="primary" size="small" @click="submitForm">查询</el-button>
						<el-button type="primary" size="small" @click="confirmUpdate">设置最新版本强制更新</el-button>
						<!-- <el-button v-permission:function="['/system/overSaleHandle/activitySku']" type="primary" size="small" @click="submitForm">查询最新版本</el-button> -->
						<!-- <el-button size="small" @click="resetForm">重置</el-button> -->
					</div>
				</el-col>
			</el-row>
		</el-form>

		

		<!-- <div class="activity-data" v-if="activity.id != undefined">
			<span>活动ID：{{activity.id}}</span>
			<span>活动商品：{{activity.productName}}</span>
			<span style="flex: 0 0 16%;">开售时间：{{activity.saleTime | forDate}}</span>
			<span style="flex: 0 0 16%;">结束时间：{{activity.endTime | forDate}}</span>
			<span style="flex: 0 0 10%;">活动状态：{{activity.status == 1 ? '待审核' : activity.status == 2 ? '审核通过' : '审核不通过'}}</span>
			<span style="flex: 0 0 10%;">结束方式：{{activity.endType == 1 ? '售罄' : activity.endType == 2 ? '中断' : '初始状态'}}</span>
			<span style="flex: 0 0 10%;">销售状态：{{activity.saleStatus == 1 ? '在售' : activity.saleStatus == 2 ? '停售' : activity.saleStatus == 3 ? '售罄' : '初始状态'}}</span>

			<div class="activity-data-edit">
				<el-button v-permission:function="['/system/overSaleHandle/activitySku/editActivity']" type="primary" size="small" @click="editActivity">编辑</el-button>
			</div>
		</div> -->

		<div class="data-table">
			<el-table :data="tableData" :empty-text="noContent" stripe border height="300">
				<el-table-column type="index" fixed label="序号" width="50"></el-table-column>
				<el-table-column label="版本标题" prop="title" width="250"></el-table-column>
				<el-table-column label="版本内容" prop="context"></el-table-column>
				<el-table-column label="发布状态" prop="isRelease" width="80"></el-table-column>
				<el-table-column label="发布时间" prop="releaseTime" width="140">
					<template slot-scope="scope">
						{{scope.row.releaseTime | forDate}}
					</template>
				</el-table-column>
				<el-table-column label="创建时间" prop="createTime" width="140">
					<template slot-scope="scope">
						{{scope.row.createTime | forDate}}
					</template>
				</el-table-column>
			</el-table>
		</div>

		<!-- 修改活动 -->
		<!-- <edit-activity :showDialog="showEditActivity" :activityData="activity" @submit="submitEditActivity" @handleClose="closeEditActivity"></edit-activity> -->

		<!-- 修改SKU -->
		<!-- <edit-sku :showDialog="showEditSku" :skuData="activitySku" @submit="submitEditSku" @handleClose="closeEditSku"></edit-sku> -->
	</div>
</template>

<script>
import { getLastestVersion, pageVersion, confirmUpdateInfo } from '@api/restaurant/sys/sysConfig'
import dayjs from 'dayjs'
// import editActivity from './editActivity'
// import editSku from './editActivitySku'
export default {
  components: {
    // editActivity,
    // editSku
  },
  data () {
    return {
      searchForm: {
    //     activityId: ''
      },
      tableData: [],
	  noContent: '暂无数据',
      loading: false
    }
  },
  filters: {
    forDate: (val) => {
      let formatTime = ''
      formatTime = val == null ? '' : dayjs(val).format('YYYY-MM-DD HH:mm:ss')
      return formatTime
    }
  },
  mounted () {
    this.getData()
  },
  methods: {
    getData () { // 获取信息
      if (this.loading) {
		  return
	  }
      this.loading = true

	  pageVersion({
        // activityId: this.searchForm.activityId
      })
        .then(res => {
          this.loading = false
          this.tableData = res.data.content
        })
        .catch(err => {
			console.log('err', err)
          	this.loading = false
        })
    },

    submitForm () { // 搜索
	  this.tableData = []
	//   this.updateRecord = {}
      this.getData()
    },
    resetForm () { // 重置搜索
      this.searchForm.activityId = ''
	  this.activity = {}
	  this.tableData = []
	  this.noContent = '暂无数据'
    },

	confirmUpdate() {
		if (this.loading) {
		  return
		}
		this.loading = true

		confirmUpdateInfo({
			// activityId: this.searchForm.activityId
		})
			.then(res => {
			this.loading = false
			})
			.catch(err => {
				console.log('err', err)
				this.loading = false
			})
	},

    editActivity () { // 编辑活动信息
      this.showEditActivity = true
    },
    submitEditActivity (form) { // 提交活动信息
      this.showEditActivity = false

      let params = {
		  activityId: form.id,
		  endTime: dayjs(form.endTime).format('YYYY-MM-DD HH:mm:ss'),
		  endType: form.endType,
		  saleStatus: form.saleStatus
	  }
	  handleActivity(params)
        .then(() => {
          this.$message.success('修改成功！')

		  // 刷新数据
		  this.activity.id = form.id
		  this.activity.endTime = form.endTime
		  this.activity.endType = form.endType
		  this.activity.saleStatus = form.saleStatus
        })
    },
    closeEditActivity () {
      this.showEditActivity = false
    },

    editSku (item) { // 编辑SKU
      this.activitySku = item
	  this.showEditSku = true
    },
    submitEditSku (form) { // 提交SKU信息
      this.showEditSku = false

      let params = {
		  activitySkuId: form.id,
		  endTime: dayjs(form.endTime).format('YYYY-MM-DD HH:mm:ss'),
		  frozenQuantity: form.frozenQuantity,
		  purchaseQuantityHad: form.purchaseQuantityHad,
		  purchaseQuantityTodo: form.purchaseQuantityTodo,
		  purchaseTotalQuantity: form.purchaseTotalQuantity,
		  unexpectedQuantity: form.unexpectedQuantity
      }

      handleSku(params)
        .then(() => {
          this.$message.success('修改成功！')

		  // 刷新数据
		  this.tableData.forEach(item => {
			  if (item.id == form.id) {
				  item.endTime = form.endTime
				  item.purchaseTotalQuantity = form.purchaseTotalQuantity
				  item.purchaseQuantityHad = form.purchaseQuantityHad
				  item.purchaseQuantityTodo = form.purchaseQuantityTodo
				  item.frozenQuantity = form.frozenQuantity
				  item.unexpectedQuantity = form.unexpectedQuantity
			  }
		  })
        })
    },
    closeEditSku () {
      this.showEditSku = false
    }
  }
}
</script>

<style lang="scss" scoped>
.data-box {
	background-color: #fff;
	border: 1px solid #ddd;
	border-radius: 4px;
	padding: 20px;

	.data-title {
		font-size: 16px;
		color: #333;
		padding: 0 0 20px 0;
	}
}
.data-search-area {
	.el-form-item {
		margin-bottom: 10px;
		::v-deep .el-form-item__content {
			line-height: 32px;
			font-size: 12px;
		}
		::v-deep .el-form-item__label {
			line-height: 32px;
			font-size: 12px;
		}
	}

	.el-input {
		font-size: 12px;
	}
	::v-deep .el-input__inner {
		height: 32px;
		line-height: 30px;
		font-size: 12px;
		border-radius: 2px;
	}
	::v-deep .el-input__icon {
		line-height: 32px;
	}
}
.search-submit {
	//justify-content: flex-end;
	display: flex;
	padding: 0 0 0 15px;
}

.activity-data {
	width: 100%;
	border-top: #eee 1px solid;
	align-items: center;
	display: flex;
	padding: 20px 10px 10px 10px;
	margin-top: 10px;
	span {
		flex: 0 0 12.5%;
		font-size: 14px;
		color: #333;
		&:first-child {
			flex: 0 0 8%;
		}
		&:nth-child(2) {
			flex: 0 0 20%;
		}
	}
	.activity-data-edit {
		flex: 0 0 8%;
		//justify-content: flex-end;
		display: flex;
	}
}

.data-table {
	width: 100%;
	margin-top: 15px;
	.el-table {
		width: 100%;
		::v-deep th {
			font-size: 12px;
			padding: 5px 0 !important;
		}
		::v-deep td {
			font-size: 12px;
			padding: 5px 0 !important;
			.cell {
				line-height: normal;
			}
		}
	}
}
</style>
